# -*- coding:UTF-8 -*-

"""
这个文件用于数据库操作，连接数据库，增删改查
"""


import pymongo

test_pr_key = "i am servie"

class db_class():
    def __init__(self): #初始化数据库，后面的函数名称就是函数功效。。
        self.conn = pymongo.MongoClient('localhost',27017)
        self.user_db = self.conn.user
    def check_user(self,username):
        cu = self.user_db.user.find({"username":username})
        return cu.count()
    def add_user(self,username,password,power):
        if(self.check_user(username)==0):
            data = {"username":username,"password":password,"power":power}
            self.user_db.user.insert(data)
            return 1
        else:
            return 0
    def change_password(self):
        pass
    def del_user(self,username):
        if(self.check_user(username)!=0):
            self.user_db.user.remove({"username":username})
            return 1
        else:
            return 0
    def get_password(self,username):
        if(self.check_user(username)!=0):
            cu = self.user_db.user.find({"username":username})
            return cu
        else:
            return 0
    def get_pr_key(self):
        return test_pr_key
    def get_uesr_power(self,username):
        if (self.check_user(username) != 0):
            cu = self.user_db.user.find({"username": username})
            return cu
        else:
            return 0
    def get_all_user_power(self):
        cu = self.user_db.user.find({})
        x = []
        for y in cu:
            x.append(str(y["username"])+"  power:  "+str(y["power"]))
        print x
        return x
    def change_power(self,username,power):
        ret = self.user_db.user.update({"username":username},{"$set":{"power":power}})
        return ret
if __name__ == '__main__':#小功能，提权
     db = db_class()
     inpt = raw_input("输入要提权的用户名")
     ret = db.change_power(inpt,"SM")
     print ret
